<?php

require_once APPLICATION_PATH.'/controllers/MotherController.php';
require_once APPLICATION_PATH.'/modules/Admin/forms/Login.php';
require_once APPLICATION_PATH.'/modules/Admin/forms/Create.php';

require_once APPLICATION_PATH.'/modules/Admin/models/User.php';

class Admin_IndexController extends MotherController
{

    public function init()
    {
        $this->view->categorie = $this->_categorie();
    }

    public function indexAction()
    {
        $this->checkAdmin();
        
        $this->_helper->layout->setLayout('layouts/2columns-left');
        $XML_Model = new Application_Model_XML();
        $dom = $XML_Model->validateDocument('facture.xml', 'facture.xsd');
        
    }

    public function createAction()
    {
        $this->_helper->layout->setLayout('layouts/login');
        $this->view->headTitle(' - Creer');
        $form = new Admin_Form_Create();

        //Validation formulaire
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
//            Zend_Debug::dump($formData);
            $this->view->value = $formData;

            if ($form->isValid($formData)) {
                //check user exist
                $XML_Model = new Application_Model_XML();
                $user = $XML_Model->userExist($formData);

//                $XML_Model->updateUser($formData);
                if($user){
                    $this->view->emailExist = "email déjà existant";
                }
                else {
                    echo 'Enregister';
                    //function save
                    $XML_Model->saveUser($formData);
                    //redirect

                }
            } else {
                //$form->populate($formData);
                $this->view->errors = ($form->populate($formData)->getErrors());
            }
        }
        $this->view->formCreate = $form;
    }

    public function loginAction()
    {
        $this->_helper->layout->setLayout('layouts/login');

        $this->view->headTitle(' - Login');
        $form = new Admin_Form_Login();

        //Validation formulaire
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                //check user exist
                $XML_Model = new Application_Model_XML();
                $user = $XML_Model->userExist($formData, TRUE);
                if($user){
                    $this->submit($user);
                    $this->_redirect('/Invoice/index');
                }
                else {
                    $this->view->errorLogin = "Login ou mot de passe incorrect";
                }
            } else {
                $form->populate($formData);
            }
        }
        $this->view->formLogin = $form;        
    }

    public function logoutAction()
    {
        if (Zend_Session::sessionExists()) {
            Zend_Session::destroy(true, true);
            $this->_redirect('/index');
        }
        //or
//        Zend_Auth::getInstance()->clearIdentity();
//        $this->_redirect('/index');
    }

    public function forgotpasswordAction()
    {
        // action body
    }

    public function resetPasswordAction()
    {
        // action body
    }

    /**
     * Submit utilisateur
     * 
     *
     */
    private function submit($user)
    {
        $session = Zend_Registry::get('session');
        # pour lire le fichier application.ini
        $options= $this->getInvokeArg('bootstrap')->getOptions();

        Zend_Session::regenerateId();
//        $users = new Model_DbTable_Utilisateur();
//        $data = array('dernier_connexion' => date('Y-m-d H:i:s'));
//
//        # Update de last_access
//        $where = $users->getAdapter()->quoteInto('email = ?', $session->username);
//        if (! $users->update($data, $where)) {
//            throw new Zend_Exception('Error on update last_access');
//        }
        /* On recupere la session et id de l' utilisateur
         * Puis on l'implémante dans la variable de $_SESSION
         */
        $session->username = $user->nom . ' '. $user->prenom;
        $session->role = (string) $user->role;
        $session->id = (string) $user->id ;
        $session->nom = (string) $user->nom ;
        $session->prenom = (string) $user->prenom ;
        
        return true;
    }

    public function erreurAction()
    {
        $this->_helper->layout->setLayout('layouts/login');
    }


}





